package com.example.testerclientapp;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignupActivity extends Activity {
	Button signup; String AccountType;
	EditText firstName, lastName, email, password, address, city, state, zip;
	private static final String SOAP_ACTION1 = "http://spartansparking.org/createAccount"; 
    private static final String METHOD_NAME1 =  "createAccount";       
    private static final String NAMESPACE =    "http://spartansparking.org/";    
    private static final String URL = "http://spartanparking.azurewebsites.net/parkingservice.asmx";   
    
    private AndroidHttpTransport mAht;
    public  SoapSerializationEnvelope mSoapEnvelope;
	//private View visitor;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup);
		signup=(Button)findViewById(R.id.sign_u);
		
		signup.setOnClickListener(new View.OnClickListener() {
        	@Override
            public void onClick(View v) 
        	{
        		  try
        	        {
        			  	//Init EditText object`
        		        firstName = (EditText)findViewById(R.id.u_type);
        		        lastName = (EditText)findViewById(R.id.editText2);
        		        email = (EditText)findViewById(R.id.editText3);
        		        password = (EditText)findViewById(R.id.editText4);
        		        address = (EditText)findViewById(R.id.editText5);
        		        city = (EditText)findViewById(R.id.editText6);
        		        state = (EditText)findViewById(R.id.editText7);
        		        zip = (EditText)findViewById(R.id.editText8);
        		        
if ((email.getText().toString().length()!= 0) && (password.getText().toString().length()!=0) && (firstName.getText().toString().length()!=0))
        		            
    {
        		        
        		        //AccountType = "1";
        		        		        		
        		        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
        		        request.addProperty("email", email.getText().toString());
        		        request.addProperty("password", password.getText().toString());
        		        request.addProperty("firstName", firstName.getText().toString());
        		        request.addProperty("lastName", lastName.getText().toString());
        		        request.addProperty("address", address.getText().toString());
        		        request.addProperty("city", city.getText().toString());
        		        request.addProperty("zip", zip.getText().toString());
        		        request.addProperty("state", state.getText().toString());
        		        request.addProperty("AccountType", "1");
        		        
        		        mSoapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        		        mSoapEnvelope.dotNet = true;
        		        mSoapEnvelope.setOutputSoapObject(request);
        		        
        		        mAht = new AndroidHttpTransport(URL);
        	        	mAht.call(SOAP_ACTION1, mSoapEnvelope);   //in-out parameter: soapEnvelope, which means the response back will be stored in this var soapEnvelope
        	        	
        	        	
        	        	 if (mSoapEnvelope.bodyIn instanceof SoapFault)
        	        	 {
        	        		 String str= ((SoapFault) mSoapEnvelope.bodyIn).faultstring;
        	        		 createAndShowDialog(str, "Sign Up Failed");
        	        		 
        	        	}
        	        	else 
        	        		 {
        	              		 SoapPrimitive resultsRequestSOAP = (SoapPrimitive)mSoapEnvelope.getResponse();
        		     
        		        		 if (resultsRequestSOAP.toString().length()!=0)
        		        		 {
        		        			 Toast.makeText(getBaseContext(), "Sign Up Successful. Your Id is"+resultsRequestSOAP.toString(), Toast.LENGTH_LONG).show();
        		        			 //createAndShowDialog(resultsRequestSOAP.toString(), "Server Response");
        		        			 Intent reservationSpotActivity = new Intent(SignupActivity.this, MainActivity.class);
        		        			 startActivity(reservationSpotActivity);
        		        		 }
        		        		 else
        		        		 	{
        		        			 	// return "false" means no user found in database
        		        			 	createAndShowDialog(resultsRequestSOAP.toString(), "Server Response");
        		        		 	}
        	        		 }
    }
    else
    {
    Toast.makeText(getApplicationContext(), "Email, Password, FirstName should not be blank", Toast.LENGTH_LONG);
    }
    
        	       }
        	       
        	       
        	        catch (Exception ex)
        	        {
        	        	ex.printStackTrace();
        	           	Toast.makeText(getBaseContext(), "Error: "+ex.getMessage(), Toast.LENGTH_LONG).show();
        	        }
             }  // end onclick signin
        }); //end onclick listener

        
        
        
        	}

	public void createAndShowDialog(String message, String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setMessage(message);
		builder.setTitle(title);
		builder.create().show();
	}
    

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.signup, menu);
		return true;
	}

}
